// sm4_ecb_demo.c
#include <stdio.h>
#include <string.h>
#include <gmssl/sm4.h>

void print_hex(const char* label, const uint8_t *data, size_t len) {
    printf("%s: ", label);
    for (size_t i = 0; i < len; i++) {
        printf("%02x", data[i]);
    }
    printf("\n");
}

int main(void) {
    // 16 字节密钥
    const uint8_t key[16] = "0123456789abcdef";
    const char *msg = "Hello, SM4!12345"; // 16 字节明文
    size_t msg_len = strlen(msg); // 必须是 16 的倍数

    uint8_t ciphertext[16];
    uint8_t plaintext[16];

    SM4_KEY sm4_key;

    // === 加密 ===
    sm4_set_encrypt_key(&sm4_key, key);
    sm4_encrypt(&sm4_key, (const uint8_t *)msg, ciphertext);
    print_hex("SM4-ECB Ciphertext", ciphertext, 16);

    // === 解密 ===
    sm4_set_decrypt_key(&sm4_key, key);  // 设置为解密密钥
    sm4_encrypt(&sm4_key, ciphertext, plaintext);  // 仍然调用 sm4_encrypt！
    plaintext[16] = '\0';
    printf("Decrypted: %s\n", plaintext);

    return 0;
}
